class PostController < ApplicationController
  def new
  	@page = "new_post"
  	@title = "New Post"

    if params[:current_path].nil?
      redirect_to root_path
    else
      params[:post][:poster_id] = @user[:user_id]
      if !params[:ext_content].nil?
        extContent = params[:ext_content]

        extImgMedia = ''
        extContent.each do |ext|
          if !ext[1][:image].nil?      
            extImgMedia += '<p style="text-align:center"><img src="' + ext[1][:image] + '" alt="" style="max-width:800px;" /></p>'
          elsif !ext[1][:media].nil?
            extImgMedia += '<p style="text-align:center" style="max-width:600px;max-height:360px;">' + ext[1][:media] + '</p>'
          end
        end
        # writelog
      logger.debug("********** Bugs **********\n#{extImgMedia.inspect}\n********** End Bugs **********")
        params[:post][:content] = params[:post][:content] + '<br/>' + extImgMedia
      end

      if !params[:id].nil?
        user_id = params[:id]
      else
        user_id = @user[:user_id]
      end

      data = { 'poster_id' => @user[:user_id], 
             'user_id' => user_id,
             'content' => params[:post][:content]
      }
      @new_post = Post.new(data)
      @new_post.save

      if !@new_post.errors.empty?
        flash[:post_warning] = @new_post.errors
      else
        flash[:post_success] = "Post successful !"
      end

      redirect_to params[:current_path]
  end
  end

  def detail
  end

  def edit
  end

  def delete
    if !params[:id].nil?
      begin
        @delete_post = Post.find(params[:id])
        @delete_post.destroy
      rescue Exception => e
        redirect_to flash[:current_page]
        return
      end
    end
    redirect_to flash[:current_page]
  end

end
